# -*- coding: utf-8 -*-
# Create your views here.
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from main.forms import RestForm, RestCuisTypeForm, RestDishForm, RestIntroImg, \
    RestAdImg
from main.models import Rest, Rest_cuis_type, Rest_dish, Rest_intro_img, \
    Rest_ad_img
from order.models import Order



def cuis_types(rest=None):
    rest_cuis_types = Rest_cuis_type.objects.filter(rest = rest,level='1')
    str = ""
    if rest_cuis_types:
        for rest_cuis_type in rest_cuis_types:
            str = str+"<li rel='%s'>%s"  % (rest_cuis_type.id,rest_cuis_type.name)
            child_rest_cuis_types = Rest_cuis_type.objects.filter(parent = rest_cuis_type.id)
            
            if child_rest_cuis_types:
                str = str + "<ul>"
                for child_rest_cuis_type in child_rest_cuis_types:
                    str = str + "<li rel='%s'>%s"  % (child_rest_cuis_type.id,child_rest_cuis_type.name)
                    str = str + "</li>"
                str = str + "</ul>"    
            str = str + "</li>"    
    return str



#餐厅信息
@login_required
def intr(request,template_name="space/rest/rest.html"):
    pass

#餐厅信息
@login_required
def updateRest(request,template_name="space/rest/rest.html",pk=None):
    rest = Rest.objects.get(pk=pk)
    form = RestForm(instance=rest)
    if request.method == "POST" :
        form = RestForm(request.POST, request.FILES)
        if form.is_valid():
            form = RestForm(request.POST, request.FILES,instance=rest)
            rest = form.save(False)
            rest.save()
            return HttpResponseRedirect("/space/rest/") 
        
    return render_to_response(template_name,{'form':form,'rest':rest},context_instance=RequestContext(request))
    
#餐厅菜式分类列表
@login_required
def cuistypes(request,template_name="space/rest/cuis_types.html"):
    rest_cuis_types = Rest_cuis_type.objects.filter(rest = Rest.objects.get(user=request.user)).order_by("-reg_time")
    return render_to_response(template_name,{'rest_cuis_types':rest_cuis_types},context_instance=RequestContext(request))

    
#餐厅菜式分类
@login_required
def addCuisType(request,template_name="space/rest/add_cuis_type.html"):
    rest = Rest.objects.get(user=request.user)
    if request.method == "POST":
        form = RestCuisTypeForm(request.POST)
        if form.is_valid():
            restCuisType = form.save(False)
            if restCuisType.parent:
                restCuisType.level = restCuisType.parent.level+1
            restCuisType.rest = rest
            restCuisType.save()
            return HttpResponseRedirect("/space/rest/cuis/")
        return render_to_response(template_name,{'form':form,'rest_cuis_types':cuis_types(rest)},context_instance=RequestContext(request))
    else:
        form = RestCuisTypeForm()
        return render_to_response(template_name,{'form':form,'rest_cuis_types':cuis_types(rest)},context_instance=RequestContext(request))

#餐厅菜式分类
@login_required
def updateCuisType(request,template_name="space/rest/update_cuis_type.html",pk=None):
    rest_cuis_type = Rest_cuis_type.objects.get(pk=pk)
    form = RestCuisTypeForm(instance=rest_cuis_type)
    if request.method == "POST":
        form = RestCuisTypeForm(request.POST)
        if form.is_valid():
            form = RestCuisTypeForm(request.POST,instance=rest_cuis_type)
            restCuisType = form.save(False)
            if restCuisType.parent:
                restCuisType.level = restCuisType.parent.level+1
            restCuisType.save()
            return HttpResponseRedirect("/space/rest/cuis/")
    return render_to_response(template_name,{'form':form,'rest_cuis_type':rest_cuis_type,'rest_cuis_types':cuis_types(rest_cuis_type.rest)},context_instance=RequestContext(request))
'''
    def getChildCuisType(parent=None):
        if parent.parent:
            for r in Rest_cuis_type.objects.filter(parent=parent):
                getChildCuisType(r)
        else: 
            parent.delete()
'''     
def delCuisType(request,pk=None):
    rest_cuis_type = Rest_cuis_type.objects.get(pk=pk)
    rest_cuis_type.delete()
    return HttpResponseRedirect("/space/rest/cuis/")
    

#餐厅菜式列表
@login_required
def dishs(request,template_name="space/rest/rest_dish.html",state="unsell"):
    rest = Rest.objects.get(user=request.user)
    if state=='sell':
        rest_dishs = Rest_dish.objects.filter(rest=rest,state=True)
    else:
        rest_dishs = Rest_dish.objects.filter(rest=rest,state=False)
    return render_to_response(template_name,{'rest_dishs':rest_dishs,'state':state},context_instance=RequestContext(request))

#餐厅菜式
@login_required
def addRestDish(request,template_name="space/rest/add_rest_dish.html"):
    rest = Rest.objects.get(user=request.user)
    if request.method == "POST":
        form = RestDishForm(request.POST, request.FILES)
        if form.is_valid():
            restdish = form.save(False)
            restdish.rest = rest
            restdish.save()
            return HttpResponseRedirect("/space/rest/dish/sell")
        return render_to_response(template_name,{'form':form,'rest_cuis_types':cuis_types(rest)},context_instance=RequestContext(request))
    else:
        form = RestDishForm(initial={'weight':1,'state':True})
        return render_to_response(template_name,{'form':form,'rest_cuis_types':cuis_types(rest)},context_instance=RequestContext(request))

#餐厅菜式
@login_required
def updateRestDish(request,template_name="space/rest/update_rest_dish.html",pk=None,state='unsell'):
    rest_dish = Rest_dish.objects.get(pk=pk)
    form = RestDishForm(instance=rest_dish)
    if request.method == "POST":
        form = RestDishForm(request.POST, request.FILES)
        if form.is_valid():
            form = RestDishForm(request.POST,request.FILES,instance=rest_dish)
            form.save()
            return HttpResponseRedirect("/space/rest/dish/"+state)
        
    return render_to_response(template_name,{'form':form,'rest_dish':rest_dish,'rest_cuis_types':cuis_types(rest_dish.rest)},context_instance=RequestContext(request))


#餐厅菜式上架与下架
def rest_dish_shell_unshell(request,pk=None,state='sell'):
    rest_dish = Rest_dish.objects.get(pk=pk)
    if state=='sell':
        rest_dish.state = True
    else:
        rest_dish.state = False
    rest_dish.save()
    return HttpResponseRedirect("/space/rest/dish/"+state)

#删除餐厅菜式
def del_rest_dish(request,pk=None,state="unsell"):
    Rest_dish.objects.get(pk=pk).delete()
    return HttpResponseRedirect("/space/rest/dish/"+state)


#餐厅图片
@login_required
def addRestIntroImg(request,pk=None):
    rest = Rest.objects.get(pk=pk)
    if request.method == "POST":
        form = RestIntroImg(request.POST, request.FILES)
        if form.is_valid():
            restIntroImg = form.save(False)
            restIntroImg.rest = rest
            restIntroImg.save()
    
    return HttpResponseRedirect("/space/rest/intr/")
@login_required
def updateRestIntroImg(request,pk=None):
    rest_intro_img = Rest_intro_img.objects.get(pk=pk)
    if request.method == "POST":
        form = RestIntroImg(request.POST, request.FILES)
        if form.is_valid():
            form = RestIntroImg(request.POST, request.FILES,instance=rest_intro_img)
            form.save()
            
    return HttpResponseRedirect("/space/rest/intr/")
@login_required
def delRestIntroImg(request,pk=None):
    Rest_intro_img.objects.get(pk=pk).delete()
    return HttpResponseRedirect("/space/rest/intr/")

#餐厅广告
@login_required
def addRestAdImg(request,pk=None):
    rest = Rest.objects.get(pk=pk)
    if request.method == "POST":
        form = RestAdImg(request.POST, request.FILES)
        if form.is_valid():
            restAdImg = form.save(False)
            restAdImg.rest = rest
            restAdImg.save()
    
    return HttpResponseRedirect("/space/rest/intr/")
@login_required
def updateRestAdImg(request,pk=None):
    rest_ad_img = Rest_ad_img.objects.get(pk=pk)
    if request.method == "POST":
        form = RestAdImg(request.POST, request.FILES)
        if form.is_valid():
            form = RestAdImg(request.POST, request.FILES,instance=rest_ad_img)
            form.save()
            
    return HttpResponseRedirect("/space/rest/intr/")
    
@login_required
def delRestAdImg(request,pk=None):
    Rest_ad_img.objects.get(pk=pk).delete()
    return HttpResponseRedirect("/space/rest/intr/")

@login_required
def rest_orders(request,template_name="space/rest/orders.html"):
    
    mess = {}
    rest = Rest.objects.filter(user = request.user)
    
    manager = Order.objects
    orders  = manager.get_query_set()
    orders = orders.filter(rest = rest)
    if request.POST.get("sn"):
        orders = orders.filter(sn = request.POST["sn"])
    orders = orders.order_by("-mod_date")
    mess['orders'] = orders
    return render_to_response(template_name, mess, context_instance=RequestContext(request))





